package com.xunji.xunji.net;

import android.text.TextUtils;

import com.huanxiao.base.base.BaseParamManager;
import com.huanxiao.base.constant.Const;
import com.huanxiao.base.util.AppConfig;
import com.huanxiao.base.util.DisplayUtil;
import com.huanxiao.util.CtxHelper;
import com.huanxiao.util.DateHelper;
import com.xunji.xunji.R;
import com.xunji.xunji.module.account.controller.UserAccount;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by paul on 16/10/19.
 */

public class ParamManager extends BaseParamManager {
    /**
     * 登录
     *
     * @param phone
     * @param password
     * @return
     */
    public static Map<String, String> loginParam(String phone, String password, String captcha) {
        Map<String, String> params = new HashMap<>();
        params.put("phone", phone);
        params.put("password", password);

        if (!TextUtils.isEmpty(captcha)) {
            params.put("captcha", captcha);
        }
        return token(params);

    }

    /**
     * 验证码
     *
     * @param phone
     * @return
     */
    public static Map<String, String> getSmscodeParam(String phone) {
        Map<String, String> params = new HashMap<>();
        params.put("phone", phone);
        return token(params);
    }

    public static Map<String, String> register(String phone, String verify, String password) {
        Map<String, String> params = new HashMap<>();
        params.put("phone", phone);
        params.put("verifyCode", verify);
        params.put("password", password);
        return token(params);
    }

    public static Map<String, String> changePswd(String oldPassword, String newPassword) {
        Map<String, String> params = new HashMap<>();
        params.put("oldPassword", oldPassword);
        params.put("newPassword", newPassword);
        return token(params);
    }

    public static Map<String, String> certification(String realName, String idNumber, String captcha) {
        Map<String, String> params = new HashMap<>();
        params.put("realName", realName);
        params.put("idNumber", idNumber);
        if (!TextUtils.isEmpty(captcha)) {
            params.put("captcha", captcha);
        }
        return token(params);
    }

    public static Map<String, String> thirdLogin(String appid, String openid, String from) {
        Map<String, String> params = new HashMap<>();
        params.put("app_id", appid);
        params.put("open_id", openid);
        params.put("from_key", from);
        return token(params);
    }


    public static Map<String, String> authVerifyPhone(String phone, String verify, String merge) {
        Map<String, String> params = new HashMap<>();
        params.put("phone", phone);
        params.put("verify", verify);
        if (!TextUtils.isEmpty(merge)) {
            params.put("merge", merge);
        }
        return token(params);
    }

    public static Map<String, String> getUserInfoParam(String phone) {
        Map<String, String> params = new HashMap<>();
        params.put("phone", phone);
        return token(params);
    }


    public static Map<String, String> uploadTrack(String nickname, double startLongitude, double startLaititude, double endLongitude, double endLatitude, Date startTime, Date endTime, String startAddress, String endAddress, String title, String trackMoveRecord, String trackPhotoRecord) {
        Map<String, String> params = new HashMap<>();
        params.put("userId", UserAccount.getInstance().getUserInfo().getUserId()+"");
        params.put("nickname", nickname==null?"未设置":nickname);
        params.put("startLongitude", startLongitude + "");
        params.put("startLatitude", startLaititude + "");
        params.put("endLongitude", endLongitude + "");
        params.put("endLatitude", endLatitude + "");
        params.put("startTime", DateHelper.convert(startTime.getTime(), DateHelper.FORMAT_YMDHMS));
        params.put("endTime", DateHelper.convert(endTime.getTime(), DateHelper.FORMAT_YMDHMS));
        params.put("startAddress", startAddress + "");
        params.put("endAddress", endAddress + "");
        params.put("title", title + "");
        params.put("trackMoveRecords", trackMoveRecord + "");
        params.put("trackPhotoRecords", trackPhotoRecord + "");

        return token(params);

    }

    public static Map<String, String> queryList(String userId, Integer page, Integer length) {
        Map<String, String> params = new HashMap<>();

        params.put("userId", userId);
        params.put("page", page + "");
        params.put("length", length + "");

        return token(params);

    }

    public static Map<String, String> onlyTokenParam() {
        return token(new HashMap<String, String>());
    }

    /**
     * app升级
     */
    public static Map<String, String> CheckUpdate() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("a", "2");
        params.put("b", AppConfig.getInstance().versionCode + "");
        return token(params);
    }

    /**
     * 更新头像信息
     */
    public static Map<String, String> refreshHeadIcon(String portrait) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("portrait", portrait);
        return token(params);
    }


    /**
     * 添加token
     *
     * @param param
     * @return
     */
    public static Map<String, String> token(Map<String, String> param) {
        if (param == null) {
            param = new HashMap<>();
        }
        if (!TextUtils.isEmpty(Const.TOKEN)) {
            param.put("token", Const.TOKEN);
        }
        return signMap(param);
    }

    public static Map<String, String> feedbackParam(String description, String images) {
        Map<String, String> params = new HashMap<>();
        params.put("description", description);
        params.put("images", images);
        return token(params);

    }

    public static Map<String, String> getDefaultRequestHeader() {
        Map<String, String> requestHeader = new HashMap<String, String>();

        StringBuffer userAgent = new StringBuffer();
        //手机厂商／手机型号
        userAgent.append(AppConfig.getPhoneModel());
        userAgent.append("/");
        userAgent.append(AppConfig.getManufacturer());
        userAgent.append("; ");
        //应用名称／应用版本
        userAgent.append(CtxHelper.getApp().getResources().getString(R.string.app_name));
        userAgent.append("/");
        userAgent.append(AppConfig.getInstance().versionName);
        userAgent.append("; ");
        //系统版本
        userAgent.append(AppConfig.getPlatform());
        userAgent.append(" ");
        userAgent.append(AppConfig.getAndroidVersion());
        userAgent.append("; ");
        //屏幕尺寸和精度
        userAgent.append(DisplayUtil.getScreenWidth() + "X" + DisplayUtil.getScreenHeight());
        userAgent.append("/");
        userAgent.append(DisplayUtil.getScreenDensity());

        try {
            requestHeader.put("User-Agent", URLEncoder.encode(userAgent.toString(), "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return requestHeader;
    }

}
